package com.wanmait.hr.controller;

import com.github.pagehelper.Page;
import com.wanmait.hr.service.*;
import com.wanmait.hr.util.MailCodeUtils;
import com.wanmait.hr.util.MailUtils;
import com.wanmait.hr.util.Pager;
import com.wanmait.hr.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private DepartmentService departmentService;
    @Resource
    private ContractTypeService contractTypeService;
    @Resource
    private UserService userService;
    @Resource
    private PostService postService;
    @Resource
    private RecordService recordService;
    @Resource
    private ContractService contractService;
    @RequestMapping("manage/addStaff/list")
    public ModelAndView list()//添加员工页面
    {
        List<Department> dm=departmentService.getDepartment();
        List<ContractType> ct=contractTypeService.getContractType();
        List<Post> posts=postService.getPost();
        ModelAndView mv=new ModelAndView();
        mv.addObject("dm",dm);
        mv.addObject("ct",ct);
        mv.addObject("posts",posts);
        return mv;

    }
    @RequestMapping("/manage/user/login")
    public  ModelAndView showLogin()
    {
        ModelAndView mv=new ModelAndView();
        return mv;
    }
    @RequestMapping("/manage/user/safetyVerification")//张思浩 通过邮箱查询用户
    public ModelAndView showSafe()
    {
        ModelAndView mv=new ModelAndView();
        return  mv;
    }
    @RequestMapping(value ="/manage/addStaff/add",method = RequestMethod.POST)
    public String  add(User user,Contract c,String time)//添加员工
    {

        userService.addStaff(user,time,c);

        return "redirect:/manage/addStaff/list";
    }
    @PostMapping("/manage/user/checkEmailName")
    @ResponseBody
    public String checkEmail(String emailName)
    {
        User user=userService.findByOneUser(emailName);
        if (user!=null)//用户名存在
        {
            return "1";
        }
        else//不存在
        {
            return"0";
        }
    }
    @PostMapping("/manage/user/sendEmailCode")
    @ResponseBody
    public void sendCode(String email, HttpServletRequest request)
    {
        User user=new User();
        user.setMail(email);
        String code=MailCodeUtils.getMailCodeUtils().achieveCode();
        boolean f= MailUtils.sendAuthCodeEmail(user, code);
        System.out.println(f);
        if (f==true)
        {

            HttpSession session=request.getSession();
            session.setAttribute("code",code);
            //session.setMaxInactiveInterval(60);
        }

    }
    @PostMapping("/manage/user/checkEmailCode")
    public  String  checkCode(String emailCode, String email, HttpServletRequest request, Model model)
    {
        HttpSession session=request.getSession();
        String code=(String)session.getAttribute("code");
        System.out.println(code);
        if (emailCode!=null&&emailCode.equals(code))
        {
            User user=userService.findByOneUser(email);
            System.out.println(user.getName());
            model.addAttribute("user",user);
            return "/manage/user/changePassword";
        }
        else
        {
            return "redirect:/manage/user/safetyVerification?mes=1";
        }
    }
    @GetMapping("/manage/user/changePassword")
    public ModelAndView showChangePassword()
    {
        ModelAndView mv=new ModelAndView();
        return  mv;
    }
    @PostMapping("/manage/user/changePassword")
    public String changePassword(User user)
    {
        userService.modifyPassword(user);
        return "redirect:/manage/user/login";
    }
    @RequestMapping("manage/roster/list")
    public ModelAndView rosterList(HttpServletRequest request)//去到员工花名册页面---陈正儒
    {
        HttpSession session= request.getSession();
                    User user=(User) session.getAttribute("user");
                    List<User> userList= userService.getStaffInfo(user);
                    ModelAndView mv=new ModelAndView();
                    mv.addObject("userList",userList);
        return mv;
    }
    @RequestMapping("manage/roster/batchDeleteUser")
    @ResponseBody
    public void batchDeleteUser(String[] id)//花名册批量删除--陈正儒
    {
        userService.batchDeleteUser(id);

    }
    @RequestMapping("manage/roster/deleteUser")
    @ResponseBody
    public String deleteUser(int id)//花名册单独删除--陈正儒
    {
       userService.deleteUser(id);
        return "1";
    }
    @RequestMapping("manage/roster/checkRecord")
    public ModelAndView checkRecord( int id){//查看个人资料--陈正儒
        Record record=new Record();
        Contract c=new Contract();
        record=recordService.checkRecord(id);
        c=contractService.checkContract(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("record",record);
        mv.addObject("c",c);
        return mv;
    }
    @RequestMapping("manage/personnelChanges/list")//进入人员异动管理--陈正儒
        public ModelAndView pcl(HttpServletRequest request)
            {

                HttpSession session= request.getSession();
                User user=(User) session.getAttribute("user");
                List<User> userList= userService.getPersonnelChanges(user);

                ModelAndView mv=new ModelAndView();
                mv.addObject("userList",userList);

                return mv;
            }

        }
